package com.qunar.crawler.worker;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.crawler.DB.JdbcUtils;
import com.qunar.crawler.utils.HumanActionSimulator4VisitingWeb;
import com.qunar.crawler.utils.TourUrlFrontier;
import com.qunar.crawler.utils.UrlFrontier;

public class CrawlerMain {

    // 抓取的时间间隔
    private static Integer tourUrlCrawlinterval = Integer.valueOf(JdbcUtils.getTourUrlCrawlinterval());
    private static Integer go117BeanCrawlinterval = Integer.valueOf(JdbcUtils.getGo117BeanCrawlinterval());
    private static final Logger logger = LoggerFactory.getLogger(CrawlerMain.class);

    /**
     * @author tao.zhang
     * @param args String[]
     */
    public static void main(String[] args) {

        logger.info("========爬虫启动========");
        final ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);

        // 抓取旅行URl的线程
        pool.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {

                 //判断是否在正常用户访问时间
                if (!HumanActionSimulator4VisitingWeb.getInstance().inInterval()) {
                    logger.info("不在正常用户访问时间,暂停抓取…………");
                } else {
                    TourUrlFrontier.fill();

                    for (int i = 0; i < 10; i++) {
                        TourCrawler tc = new TourCrawler();
                       pool.submit(tc);
                    }

                }

            }

        }, 5, 60*15, TimeUnit.SECONDS);

        // 根据旅行URl抓取117GO实体线程

        pool.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
            	
            	//add 117go live page urls to UrlFroniter 4 Crawler, count :20000,
            	UrlFrontier.fill();
//                if (!HumanActionSimulator4VisitingWeb.getInstance().inInterval()) {
//                    logger.info("不在正常用户访问时间,暂停抓取…………");
//                } else {

                    for (int i = 0; i < 10; i++) {
                        Go117Crawler crawler = new Go117Crawler(i);
                        pool.submit(crawler);
                    }


//                }

            }

        }, 10, 60*60*24, TimeUnit.SECONDS);

    }
}
